﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using Microsoft.Win32;
using System.Windows;
using System.Xml;

namespace SHomeWorkshop.LunarMind.Commands
{
    /// <summary>
    /// 以此类为模板，制作自定义命令类
    ///     共需要修改１０处！！！
    /// ==============================
    /// 
    /// 清除标题文本命令。
    /// ★①，修改类名。
    /// 
    /// 此命令用于展开除根标题以外的所有标题，以便进行演示。（根标题是不能折叠的，无所谓展开。）
    /// </summary>
    public static class ExpandAllTitlesCommand
    {
        private static RoutedUICommand routedUICmd;//创建私有字段

        public static RoutedUICommand RoutedUICmd//创建只读静态属性
        {
            get { return routedUICmd; }
        }

        /// <summary>
        /// ★②，修改静态构造方法名。
        /// </summary>
        static ExpandAllTitlesCommand()//类型构造器
        {
            //★③，修改两个字符串参数名。★④以及第三个参数的类型名。
            routedUICmd = new RoutedUICommand("ExpandAllTitlesCommand", "ExpandAllTitlesCommand",
                typeof(CollapseAllTitlesCommand),//创建RoutedUICommand对象
                new InputGestureCollection() 
                { 
                    //★⑤，修改此处两个参数，以便挂接快捷键。
                    new KeyGesture(Key.F5,ModifierKeys.Alt|ModifierKeys.Shift,"Alt+Shift+F5")
                });

            cmdBinding.Command = routedUICmd;
            cmdBinding.CanExecute += new CanExecuteRoutedEventHandler(cmdBinding_CanExecute);
            cmdBinding.Executed += new ExecutedRoutedEventHandler(cmdBinding_Executed);
        }

        private static CommandBinding cmdBinding = new CommandBinding();

        public static CommandBinding CmdBinding
        {
            get { return cmdBinding; }
        }

        static void cmdBinding_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            //★⑦，修改此方法的实现。

            ColapseAllTitles();
        }

        /// <summary>
        /// 此方法是调用DeleteCommand的Delete来实现。
        /// </summary>
        internal static void ColapseAllTitles()
        {
            ModifingItem mi = new ModifingItem()
            {
                IsNeedRefreshTitleAndLinesLocation = true,
                ModifingName = "切换展开/折叠"
            };

            foreach (UIElement ue in Globals.MainWindow.mainCanvas.Children)
            {
                Title t = ue as Title;
                if (t == null) continue;
                if (t.IsRootTitle) continue;
                if (t.CollapsedLowers == false) continue;

                if (t.GetLowerLevelTitles().Count <= 0) continue;

                Action act = new Action(t.ID, -1, t.GetType().Name,
                    "CollapsedLowers", true.ToString(), false.ToString());

                t.CollapsedLowers = false;

                mi.Add(act);
            }

            if (mi.ModifiedPropertyAndValues.Count <= 0)
            {
                MessageBox.Show("　　没有任何部件可以展开！（根标题不允许折叠，无须展开。）", Globals.AppName,
                    MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }

            Globals.MainWindow.Do(mi);
            Globals.MainWindow.IsModified = true;
            Globals.MainWindow.RefreshTitleAndLinesLocation();

            if (Globals.MainWindow.MainSelectedTitle != null)
            {
                Title.TryToShowTitleInVisualArea(Globals.MainWindow.MainSelectedTitle, false);
            }
        }

        static void cmdBinding_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            //★⑧，修改此方法的实现。
            e.CanExecute = true;//除根标题外其余均可折叠，总是可以执行。
        }
    }
}
